package com.hrhx.mongodb;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;

public class MongoDBUtil {
	
	
	/**
	 * 获取MongoDB数据库实例
	 * @return DB
	 */
	@SuppressWarnings("deprecation")
	public DB getMongoDB(String url,int port,String database){
		MongoClient mongoClient = new MongoClient(url, port);
		return  mongoClient.getDB(database);
	}
	
	private DB getMongoDB(){
		return this.getMongoDB("localhost", 27017, "PowerPredictionDB");
	}
	
	/**
	* 对连接进行认证
	*/
	@SuppressWarnings("unused")
	private MongoClient getMongoClient(){
		/**
		 *  使用MongoClient来连接MongoDB
		 *	MongoClient是线程安全的，可以在多程程环境中共享同一个MongoClient。
		 *  通常来说，一个应用程序中，只需要生成一个全局的MongoClient实例，
		 *	然后在程序的其他地方使用这个实例即可
		 */
		String sURI = String.format("mongodb://%s:%s@%s:%d/%s", "duhongming", "123456", "localhost", 27017, "mydb"); 
		MongoClientURI uri = new MongoClientURI(sURI);
		return new MongoClient(uri);
	}
	
	/**
	 * 保存
	 * @param table
	 * @param doc
	 */
	public void insert(String table,BasicDBObject doc){
		// 获取一个集合
		DBCollection  col = this.getMongoDB().getCollection(table);
		col.insert(doc);
	}
	
	/**
	 * 更新
	 * @param table
	 * @param query
	 * @param up
	 */
	public void update(String table,BasicDBObject query,BasicDBObject up){
		// 获取一个集合
		DBCollection  col = this.getMongoDB().getCollection(table);
		col.update(query, up);
	}
	
	/**
	 * 查找一条数据
	 * @param table
	 * @return
	 */
	public DBObject getOne(String table){
		// 获取一个集合
		DBCollection  col = this.getMongoDB().getCollection(table);
		return col.findOne();
	}
	
	/**
	 * 根据条件，查找所有数据
	 * @param table
	 * @return
	 */
	public DBCursor getAll(String table,BasicDBObject query){
		// 获取一个集合
		DBCollection  col = this.getMongoDB().getCollection(table);
		return col.find(query);
	}
	
	/**
	 * 删除
	 * @param table
	 * @param query
	 */
	public void delete(String table,BasicDBObject query){
		// 获取一个集合
		DBCollection  col = this.getMongoDB().getCollection(table);
		col.remove(query);
	}
	
	//增
	//@Test
	public void testSave(){
		/**
		 * { “name”: “mongo”, “info”: { “ver”: “3.0” } }
			现在需要插入到集合mycol中。为了插入到集合中，可以使用BasicDBObject构造一个文档
		 */
		BasicDBObject doc = new BasicDBObject("name", "mongo").append("info", new BasicDBObject("ver", "3.0"));
		this.insert("mycol", doc);
		for (int i=0; i < 100; i++) {
			this.insert("mycol",new BasicDBObject("i", i));
		}
	}
	
	//查一
	//@Test
	public void testGetOne(){
		System.out.println(this.getOne("mycol"));
	}
	
	//查多
	@Test
	public void testGetAll(){
		//BasicDBObject query = new BasicDBObject("i", 71);
		BasicDBObject query = new BasicDBObject("i", new BasicDBObject("$gte", 50));
		DBCursor cursor = this.getAll("mycol",query);
		try {
		   while(cursor.hasNext()) {
		       System.out.println(cursor.next());
		   }
		} finally {
			   cursor.close();
		}
	}
	
	//更新
	//@Test
	public void testUpdate(){
		BasicDBObject query = new BasicDBObject("i", 70);
		BasicDBObject up = new BasicDBObject("$set", new BasicDBObject("i", 100));
		this.update("mycol", query, up);
		
	}
	
	//删除
	//@Test
	public void testRemove(){
		BasicDBObject query = new BasicDBObject("i", 100);
		this.delete("mycol", query);
	}
	
	@Test
	public void testPic() throws IOException{
		DB db= this.getMongoDB();
		GridFS myFS = new GridFS(db,"PowerPredictionDB111"); //创建GridFS   对mongoDBFile数据库中的user1进行操作，这样文件的读取和删除都是user1中的文件  
		File d0 = new File("E:/data/data我");
		File[] dstr0 = d0.listFiles();
		for(File f0:dstr0){
			File d1 =new File(f0.getAbsolutePath());
			File[] dstr1 = d1.listFiles();
			for(File f1:dstr1){
				//保存文件  
				GridFSFile file = myFS.createFile(f1); 
				BasicDBObject doc = new BasicDBObject("name", "mongo").append("info", new BasicDBObject("ver", "3.0"));
				file.put("a", doc);
				System.out.println(f1.getAbsolutePath());
		        file.save();  
			}
		}     
		//输出文件  
        GridFSDBFile file =myFS.findOne("Meteologica_SSPSS-Wangzishan_201504061830_weather.csv"); 
        file.writeTo(new File("D:/Meteologica_SSPSS-Wangzishan_201504061830_weather.csv"));
	}
	
	
	
}
